Abstract block-attach and block-detach calls, and add to that the correct
authoremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Sun, 7 May 2006 12:34:45 +0000 (13:34 +0100)
committeremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Sun, 7 May 2006 12:34:45 +0000 (13:34 +0100)
polling waiting for the device to connect and disconnect.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
15 files changed:
tools/xm-test/lib/XmTestLib/block_utils.py [new file with mode: 0644]
tools/xm-test/tests/block-create/01_block_attach_device_pos.py
tools/xm-test/tests/block-create/02_block_attach_file_device_pos.py
tools/xm-test/tests/block-create/04_block_attach_device_repeatedly_pos.py
tools/xm-test/tests/block-create/05_block_attach_and_dettach_device_repeatedly_pos.py
tools/xm-test/tests/block-create/07_block_attach_baddevice_neg.py
tools/xm-test/tests/block-create/08_block_attach_bad_filedevice_neg.py
tools/xm-test/tests/block-create/09_block_attach_and_dettach_device_check_data_pos.py
tools/xm-test/tests/block-create/10_block_attach_dettach_multiple_devices.py
tools/xm-test/tests/block-destroy/01_block-destroy_btblock_pos.py
tools/xm-test/tests/block-destroy/02_block-destroy_rtblock_pos.py
tools/xm-test/tests/block-destroy/05_block-destroy_byname_pos.py
tools/xm-test/tests/block-destroy/06_block-destroy_check_list_pos.py
tools/xm-test/tests/block-list/02_block-list_attachbd_pos.py
tools/xm-test/tests/block-list/06_block-list_checkremove_pos.py

diff --git a/tools/xm-test/lib/XmTestLib/block_utils.py b/tools/xm-test/lib/XmTestLib/block_utils.py
new file mode 100644 (file)
index 0000000..38c5d20
--- /dev/null
@@ -0,0 +1,53 @@
+#!/usr/bin/python
+
+# Copyright (c) 2006 XenSource Inc.
+# Author: Ewan Mellor <ewan@xensource.com>
+
+import time
+
+from XmTestLib import *
+
+import xen.util.blkif
+
+
+__all__ = [ "block_attach", "block_detach" ]
+
+
+def get_state(domain, devname):
+    number = xen.util.blkif.blkdev_name_to_number(devname)
+    s, o = traceCommand("xm block-list %s | awk '/^%d/ {print $4}'" %
+                        (domain.getName(), number))
+    if s != 0:
+        FAIL("block-list failed")
+    if o == "":
+        return 0
+    else:
+        return int(o)
+
+
+def block_attach(domain, phy, virt):
+    status, output = traceCommand("xm block-attach %s %s %s w" %
+                                  (domain.getName(), phy, virt))
+    if status != 0:
+        FAIL("xm block-attach returned invalid %i != 0" % status)
+
+    for i in range(10):
+        if get_state(domain, virt) == 4:
+            break
+        time.sleep(1)
+    else:
+        FAIL("block-attach failed: device did not switch to Connected state")
+
+
+def block_detach(domain, virt):
+    status, output = traceCommand("xm block-detach %s %s" %
+                                  (domain.getName(), virt))
+    if status != 0:
+        FAIL("xm block-detach returned invalid %i != 0" % status)
+
+    for i in range(10):
+        if get_state(domain, virt) == 0:
+            break
+        time.sleep(1)
+    else:
+        FAIL("block-detach failed: device did not disappear")
index 492d83f2cfc942e3663b580d736aa5539d65c5f9..13e6a5efb876e8f05d72e7e66842e1b053947e87 100644 (file)
@@ -3,11 +3,10 @@
 # Copyright (C) International Business Machines Corp., 2005
 # Author: Murillo F. Bernardes <mfb@br.ibm.com>
 
-import sys
 import re
-import time
 
 from XmTestLib import *
+from XmTestLib.block_utils import *
 
 if ENABLE_HVM_SUPPORT:
     SKIP("Block-attach not supported for HVM domains")
@@ -33,9 +32,7 @@ except ConsoleError, e:
     FAIL(str(e))
     
 
-status, output = traceCommand("xm block-attach %s phy:ram1 sdb1 w" % domain.getName())
-if status != 0:
-        FAIL("xm block-attach returned invalid %i != 0" % status)
+block_attach(domain, "phy:ram1", "sdb1")
 
 try:   
        run = console.runCmd("cat /proc/partitions")
index df3f0d67a619ced320e9edbce28dd6f7b787e11c..b4e7c6974ab829b3f7ddc5268fee2e7655c18cc0 100644 (file)
@@ -3,11 +3,10 @@
 # Copyright (C) International Business Machines Corp., 2005
 # Author: Murillo F. Bernardes <mfb@br.ibm.com>
 
-import sys
 import re
-import time
 
 from XmTestLib import *
+from XmTestLib.block_utils import *
 
 if ENABLE_HVM_SUPPORT:
     SKIP("Block-attach not supported for HVM domains")
@@ -33,9 +32,7 @@ except ConsoleError, e:
     FAIL(str(e))
     
 
-status, output = traceCommand("xm block-attach %s file:/dev/ram1 sdb2 w" % domain.getName())
-if status != 0:
-        FAIL("xm block-attach returned invalid %i != 0" % status)
+block_attach(domain, "file:/dev/ram1", "sdb2")
 
 try:
        run = console.runCmd("cat /proc/partitions")
index a3b9785785d91b31ee4bdf6d202f4d7bc05358a9..6d9eb513fe2f1911fe84dcf22b5117dcbd42d6f1 100644 (file)
@@ -3,9 +3,7 @@
 # Copyright (C) International Business Machines Corp., 2005
 # Author: Murillo F. Bernardes <mfb@br.ibm.com>
 
-import sys
 import re
-import time
 
 from XmTestLib import *
 
index 7f7fe77ead55ad7da0557c81c2b51c55f69b845b..db406fecd474f27677b38ffcb8d1d313afb087c5 100644 (file)
@@ -3,11 +3,10 @@
 # Copyright (C) International Business Machines Corp., 2005
 # Author: Murillo F. Bernardes <mfb@br.ibm.com>
 
-import sys
 import re
-import time
 
 from XmTestLib import *
+from XmTestLib.block_utils import *
 
 if ENABLE_HVM_SUPPORT:
     SKIP("Block-attach not supported for HVM domains")
@@ -33,18 +32,12 @@ except ConsoleError, e:
     
 
 for i in range(10):
-       status, output = traceCommand("xm block-attach %s phy:ram1 sdb1 w" % domain.getName())
-       if status != 0:
-               FAIL("xm block-attach returned invalid %i != 0" % status)
-       # verify that it comes
+       block_attach(domain, "phy:ram1", "sdb1")
        run = console.runCmd("cat /proc/partitions")
        if not re.search("sdb1", run["output"]):
                FAIL("Failed to attach block device: /proc/partitions does not show that!")
                
-       status, output = traceCommand("xm block-detach %s 2065" % domain.getName())
-       if status != 0:
-               FAIL("xm block-detach returned invalid %i != 0" % status)
-       # verify that it goes
+       block_detach(domain, "sdb1")
        run = console.runCmd("cat /proc/partitions")
        if re.search("sdb1", run["output"]):
                FAIL("Failed to dettach block device: /proc/partitions still showing that!")
index 2c6b19e54aec05845cc96470b4f1f3606bc01c0f..f2043d7dd3b907af14ab786ec8c5fbab4c1bb76e 100644 (file)
@@ -3,9 +3,7 @@
 # Copyright (C) International Business Machines Corp., 2005
 # Author: Murillo F. Bernardes <mfb@br.ibm.com>
 
-import sys
 import re
-import time
 
 from XmTestLib import *
 
index 8c129bd2a349cbc78d1bafd43fe750f587580af4..b1c776d71a25bb83127783d4b09544d20da978e7 100644 (file)
@@ -3,9 +3,7 @@
 # Copyright (C) International Business Machines Corp., 2005
 # Author: Murillo F. Bernardes <mfb@br.ibm.com>
 
-import sys
 import re
-import time
 
 from XmTestLib import *
 
index bcfb4fe26c7b3ef9420c547dbe1d408b9cc90d3b..b997379a25d24afc703cb9b3bc4336a5fdb0417b 100644 (file)
@@ -3,11 +3,10 @@
 # Copyright (C) International Business Machines Corp., 2005
 # Author: Murillo F. Bernardes <mfb@br.ibm.com>
 
-import sys
 import re
-import time
 
 from XmTestLib import *
+from XmTestLib.block_utils import *
 
 if ENABLE_HVM_SUPPORT:
     SKIP("Block-attach not supported for HVM domains")
@@ -36,10 +35,7 @@ if s != 0:
     FAIL("mke2fs returned %i != 0" % s)
 
 for i in range(10):
-       status, output = traceCommand("xm block-attach %s phy:ram1 hda1 w" % domain.getName())
-       if status != 0:
-               FAIL("xm block-attach returned invalid %i != 0" % status)
-       # verify that it comes
+       block_attach(domain, "phy:ram1", "hda1")
        run = console.runCmd("cat /proc/partitions")
        if not re.search("hda1", run["output"]):
                FAIL("Failed to attach block device: /proc/partitions does not show that!")
@@ -56,10 +52,7 @@ for i in range(10):
        print run['output']
        console.runCmd("umount /mnt/hda1")
        
-       status, output = traceCommand("xm block-detach %s 769" % domain.getName())
-       if status != 0:
-               FAIL("xm block-detach returned invalid %i != 0" % status)
-       # verify that it goes
+       block_detach(domain, "hda1")
        run = console.runCmd("cat /proc/partitions")
        if re.search("hda1", run["output"]):
                FAIL("Failed to dettach block device: /proc/partitions still showing that!")
index b538e3d93641ea9cd8d117a020fe3a5454bc48cf..833f75c21a295319e3321096b93b69ba8d4b77dd 100644 (file)
@@ -3,15 +3,14 @@
 # Copyright (C) International Business Machines Corp., 2005
 # Author: Murillo F. Bernardes <mfb@br.ibm.com>
 
-import sys
 import re
-import time
 import random
 from xen.util import blkif
 
 from os import path.basename
 
 from XmTestLib import *
+from XmTestLib.block_utils import *
 
 def availableRamdisks():
     i = 0
@@ -21,11 +20,7 @@ def availableRamdisks():
     return i
 
 def attach(phy, devname):
-    # Attach 
-    status, output = traceCommand("xm block-attach %s phy:%s %s w" % (domain.getName(), phy, devname))
-    if status != 0:
-       return -1, "xm block-attach returned invalid %i != 0" % status
-       
+    block_attach(domain, "phy:%s" % phy, devname)
     run = console.runCmd("cat /proc/partitions")
     if not re.search(basename(devname), run["output"]):
         return -2, "Failed to attach block device: /proc/partitions does not show that!"
@@ -33,16 +28,12 @@ def attach(phy, devname):
     return 0, None
 
 
-def dettach(devname):
-    devnum = blkif.blkdev_name_to_number(devname)
-    
-    status, output = traceCommand("xm block-detach %s %d" % (domain.getName(), devnum))
-    if status != 0:
-        return -1, "xm block-attach returned invalid %i != 0" % status
+def detach(devname):
+    block_detach(domain, devname)
 
     run = console.runCmd("cat /proc/partitions")
     if re.search(basename(devname), run["output"]):
-        return -2, "Failed to dettach block device: /proc/partitions still showing that!"
+        return -2, "Failed to detach block device: /proc/partitions still showing that!"
 
     return 0, None
        
@@ -91,7 +82,7 @@ while i < ramdisks or devices:
         devname = random.choice(devices)
        devices.remove(devname)
        print "Detaching %s" % devname
-       status, msg = dettach(devname)
+       status, msg = detach(devname)
        if status:
            FAIL(msg)
 
index 1e465b3c3729443d0e07b10bee344a2eaa89d287..835dfa9b1d77c7d6f721ff224504c1e6361e03cd 100644 (file)
@@ -4,6 +4,7 @@
 # Author: Dan Smith <danms@us.ibm.com>
 
 from XmTestLib import *
+from XmTestLib.block_utils import block_detach
 
 if ENABLE_HVM_SUPPORT:
     SKIP("Block-detach not supported for HVM domains")
@@ -28,11 +29,7 @@ except ConsoleError, e:
 if run["return"] != 0:
     FAIL("block device isn't attached; can't detach!")
 
-status, output = traceCommand("xm block-detach %s 769" % domain.getName(),
-                              logOutput=True)
-if status != 0:
-    FAIL("block-detach returned invalid %i != 0" % status)
-
+block_detach(domain, "hda1")
 try:
 
     run = console.runCmd("cat /proc/partitions | grep hda1")
index 4055c7d887fa1d476fa1b8e39e99489c8937311d..47ff9a6fe511fa86d3676f3ef2c57fd63088a9a1 100644 (file)
@@ -4,6 +4,7 @@
 # Author: Dan Smith <danms@us.ibm.com>
 
 from XmTestLib import *
+from XmTestLib.block_utils import *
 
 if ENABLE_HVM_SUPPORT:
     SKIP("Block-detach not supported for HVM domains")
@@ -17,11 +18,7 @@ except DomainError, e:
         print e.extra
     FAIL("Unable to create domain")
 
-status, output = traceCommand("xm block-attach %s phy:/dev/ram0 hda1 w" % domain.getName())
-if status != 0:
-    FAIL("Failed to attach block device")
-    pass
-
+block_attach(domain, "phy:/dev/ram0", "hda1")
 try:
     run = console.runCmd("cat /proc/partitions | grep hda1")
 except ConsoleError, e:
@@ -31,10 +28,7 @@ except ConsoleError, e:
 if run["return"] != 0:
     FAIL("Failed to verify that block dev is attached")
 
-status, output = traceCommand("xm block-detach %s 769" % domain.getName())
-if status != 0:
-    FAIL("block-detach returned invalid %i != 0" % status)
-
+block_detach(domain, "hda1")
 try:
     run = console.runCmd("cat /proc/partitions | grep hda1")
 except ConsoleError, e:
index 3f43fe9d95337b36b94c58965406ab064f6b3244..d77e587c33c5508c7b193c120fb2cc877967031f 100644 (file)
@@ -4,6 +4,7 @@
 # Author: Dan Smith <danms@us.ibm.com>
 
 from XmTestLib import *
+from XmTestLib.block_utils import block_detach
 
 if ENABLE_HVM_SUPPORT:
     SKIP("Block-detach not supported for HVM domains")
@@ -27,11 +28,7 @@ except ConsoleError, e:
 if run["return"] != 0:
     FAIL("block device isn't attached; can't detach!")
 
-status, output = traceCommand("xm block-detach %s hda1" % domain.getName(),
-                              logOutput=True)
-if status != 0:
-    FAIL("block-detach returned invalid %i != 0" % status)
-
+block_detach(domain, "hda1")
 try:
 
     run = console.runCmd("cat /proc/partitions | grep hda1")
index 25bba2183cc0aeef1b1faf58d628ea6b17e8b08c..38d60a92c1364b44b2f4991d57f990908e46380d 100644 (file)
@@ -4,19 +4,10 @@
 # Author: Dan Smith <danms@us.ibm.com>
 
 from XmTestLib import *
+from XmTestLib.block_utils import *
 
-import time
 import re
 
-def checkBlockList(domain):
-    s, o = traceCommand("xm block-list %s" % domain.getName())
-    if s != 0:
-        FAIL("block-list failed")
-    if re.search("769", o):
-        return True
-    else:
-        return False
-
 def checkXmLongList(domain):
     s, o = traceCommand("xm list --long %s" % domain.getName())
     if s != 0:
@@ -26,12 +17,6 @@ def checkXmLongList(domain):
     else:
         return False
 
-def checkBlockState(domain):
-    s, o = traceCommand("xm block-list %s | awk '{print $4}' |tail -n 1" % domain.getName())
-    if s != 0:
-        FAIL("block-list failed")
-    return int(o)
-
 if ENABLE_HVM_SUPPORT:
     SKIP("Block-detach not supported for HVM domains")
 
@@ -42,32 +27,12 @@ try:
 except DomainError,e:
     FAIL(str(e))
 
-s, o = traceCommand("xm block-attach %s phy:/dev/ram0 hda1 w" % domain.getName())
-if s != 0:
-    FAIL("block-attach failed")
-
-if not checkBlockList(domain):
-    FAIL("block-list does not show that hda1 was attached")
+block_attach(domain, "phy:/dev/ram0", "hda1")
 
 if not checkXmLongList(domain):
     FAIL("xm long list does not show that hda1 was attached")
 
-for i in range(1, 10):
-    time.sleep(1)
-    state = checkBlockState(domain)
-    if state == 4:
-        break
-
-s, o = traceCommand("xm block-detach %s hda1" % domain.getName())
-if s != 0:
-    FAIL("block-detach failed")
-
-time.sleep(2)
-
-if checkBlockList(domain):
-    FAIL("block-list does not show that hda1 was removed")
+block_detach(domain, "hda1")
 
 if checkXmLongList(domain):
     FAIL("xm long list does not show that hda1 was removed")
-
-
index c189dbe335329187a6cd2f6374300b5b6d9f3720..256b44d951b03cf8b3b399a7b8c1d1e9c7377bfe 100644 (file)
@@ -1,5 +1,5 @@
 #!/usr/bin/python
-                                                                                              
+
 # Copyright (C) International Business Machines Corp., 2005
 # Author: Li Ge <lge@us.ibm.com)
 
@@ -7,12 +7,13 @@
 
 
 from XmTestLib import *
+from XmTestLib.block_utils import block_attach
 
 if ENABLE_HVM_SUPPORT:
     SKIP("Block-list not supported for HVM domains")
 
 domain = XmTestDomain()
-                                                                                              
+
 try:
     console = domain.start()
 except DomainError, e:
@@ -21,9 +22,7 @@ except DomainError, e:
     FAIL("Unable to create domain")
 
 #Attach one virtual block device to domainU
-status, output = traceCommand("xm block-attach %s phy:/dev/ram0 hda1 w" % domain.getId())
-if status != 0:
-    FAIL("Fail to attach block device")
+block_attach(domain, "phy:/dev/ram0", "hda1")
 
 #Verify block-list on Domain0
 status, output = traceCommand("xm block-list %s" % domain.getId())
index 5cba6f49b50d07a96b5e643ae9fb564ab0fc922f..db2c54104db89cd5b0b63fff6bf362b7e2e3715a 100644 (file)
@@ -4,6 +4,7 @@
 # Author: Dan Smith <danms@us.ibm.com>
 
 from XmTestLib import *
+from XmTestLib.block_utils import *
 
 if ENABLE_HVM_SUPPORT:
     SKIP("Block-list not supported for HVM domains")
@@ -21,9 +22,7 @@ if s != 0:
 if o:
     FAIL("block-list without devices reported something!")
 
-s, o = traceCommand("xm block-attach %s phy:/dev/ram0 hda1 w" % domain.getName())
-if s != 0:
-    FAIL("Unable to attach /dev/ram0->hda1")
+block_attach(domain, "phy:/dev/ram0", "hda1")
 
 s, o = traceCommand("xm block-list %s" % domain.getName())
 if s != 0:
@@ -31,9 +30,7 @@ if s != 0:
 if o.find("769") == -1:
     FAIL("block-list didn't show the block device I just attached!")
 
-s, o = traceCommand("xm block-attach %s phy:/dev/ram1 hda2 w" % domain.getName())
-if s != 0:
-    FAIL("Unable to attach /dev/ram1->hda2")
+block_attach(domain, "phy:/dev/ram1", "hda2")
 
 s, o = traceCommand("xm block-list %s" % domain.getName())
 if s != 0:
@@ -41,11 +38,8 @@ if s != 0:
 if o.find("770") == -1:
     FAIL("block-list didn't show the other block device I just attached!")
 
-s, o = traceCommand("xm block-detach %s 769" % domain.getName())
-if s != 0:
-    FAIL("block-detach of hda1 failed")
+block_detach(domain, "hda1")
 
-time.sleep(1)
 s, o = traceCommand("xm block-list %s" % domain.getName())
 if s != 0:
     FAIL("block-list failed after detaching a device")
@@ -54,11 +48,8 @@ if o.find("769") != -1:
 if o.find("770") == -1:
     FAIL("hda2 not shown after detach of hda1!")
 
-s, o = traceCommand("xm block-detach %s 770" % domain.getName())
-if s != 0:
-    FAIL("block-detach of hda2 failed")
+block_detach(domain, "hda2")
 
-time.sleep(1)
 s, o = traceCommand("xm block-list %s" % domain.getName())
 if s != 0:
     FAIL("block-list failed after detaching another device")